// ContactsForm.cpp : Implementation of CContactsForm

#include "stdafx.h"
#include "ContactsForm.h"
#include <stdexcept>
#include "../commonlib/Log.h"

// CContactsForm

void CContactsForm::LoadContactForm()
{
	ATLASSERT(m_app);	
	HWND hListView = ::GetDlgItem(m_hWnd,IDC_LIST_CONTACT);
	InitListViewUI(hListView);
	LogDebug(_T("CContactsForm::LoadContactForm"));
	Outlook::_NameSpacePtr ns = m_app->GetNamespace(L"MAPI");
	LogDebug(_T("After GetNamespace"));
	Outlook::MAPIFolderPtr contactFolder = ns->GetDefaultFolder(Outlook::olFolderContacts);
	LogDebug(_T("After GetDefaultFolder(Outlook::olFolderContacts)"));
	Outlook::_ItemsPtr items = contactFolder->Items;
	long itemCount;
	itemCount= items->GetCount();
	LogDebug(_T("After GetCount"));
	long index = 0;
	//LPWSTR mobile;
	//LPWSTR fullname;
	std::wstring fullname;
	std::wstring mobile;
	for( long i = 0 ; i < itemCount ; i++ )
	{
		Outlook::_ContactItemPtr item = NULL;
		if(i == 0) item = items->GetFirst();
		else item = items->GetNext();
		LogDebug(_T("After Get _ContactItemPtr"));
		fullname.clear();
		mobile.clear();
		if(item == NULL){
			LogDebug(_T("Get a NULL _ContactItemPtr"));
			continue;
		}
		try{
			if(item->FullName.GetBSTR() != NULL)  
			{
				LogDebug(_T("Take FullName"));
				fullname = item->FullName;
			}
			else if(item->FirstName.GetBSTR() != NULL)
			{
				LogDebug(_T("Take FirstName"));
				fullname = item->FirstName;
			}
			else if(item->LastName.GetBSTR() != NULL)
			{
				LogDebug(_T("Take LastName"));
				fullname = item->LastName;
			}
			else if(item->MiddleName.GetBSTR() != NULL)
			{
				LogDebug(_T("Take MiddleName"));
				fullname = item->MiddleName;
			}
			else
			{
				LogDebug(_T("Take None"));
				continue;
			}
		}
		catch(...){
			LogDebug(_T("One of _ContactItemPtr Name API is Invalid!"));
			continue;
		}
		//fullname = (LPWSTR)item->
		try{
			if(item->MobileTelephoneNumber.GetBSTR() != NULL){
				LogDebug(_T("Take MobileTelephoneNumber"));
				mobile = item->MobileTelephoneNumber;
			}
		}
		catch(...){
			LogDebug(_T("_ContactItemPtr.MobileTelephoneNumber API is Invalid!"));
			continue;
		}
		if(mobile.empty()){
			LogDebug(_T("Take MobileTelephoneNumber Empty"));
			continue;
		}

		LogDebug(_T("Go Through One valid ContactItem!"));
		this->InsertOneContactInListView(hListView,index++,fullname.c_str(),mobile.c_str());
	}
}

void CContactsForm::InitListViewUI(HWND hListView)
{
	ATLASSERT(hListView);
	LVCOLUMNW col;

	//Extended Styles
	ListView_SetExtendedListViewStyle(hListView, LVS_EX_FULLROWSELECT);
	//Color
	ListView_SetTextColor(hListView, RGB(64, 128, 128));

	//Column Headers
	col.mask = LVCF_TEXT | LVCF_WIDTH;
	col.pszText = L"Name";
	col.cx = 200;
	ListView_InsertColumn(hListView, 0, &col);

	col.pszText = L"Mobile";
	col.cx = 200;
	ListView_InsertColumn(hListView, 1, &col);
	ListView_DeleteAllItems(hListView);
}

void CContactsForm::InsertOneContactInListView(HWND hList, int index ,  LPCWSTR fullName , LPCWSTR mobile)
{
	ATLASSERT(hList);
	LVITEMW lvi;
	lvi.iItem = index;
	lvi.mask = LVIF_TEXT | LVIF_PARAM;
	lvi.iSubItem = 0;
	lvi.pszText = const_cast<WCHAR*>(fullName);
	ListView_InsertItem(hList, &lvi);
	lvi.mask = LVIF_TEXT;
	lvi.iSubItem = 1;
	lvi.pszText = const_cast<WCHAR*>(mobile);
	ListView_SetItem(hList, &lvi);
}

//button To-> click
LRESULT CContactsForm::OnBnClickedButton1(WORD /*wNotifyCode*/, WORD /*wID*/, HWND /*hWndCtl*/, BOOL& /*bHandled*/)
{
	return EnumSelectitemInContactList();
}
LRESULT CContactsForm::OnNMDblclkListContact(int /*idCtrl*/, LPNMHDR pNMHDR, BOOL& /*bHandled*/)
{
	return EnumSelectitemInContactList();
}

void CContactsForm::AppendMobileNumberToBox(LPCWSTR append)
{
	HWND hEditToBox = ::GetDlgItem(m_hWnd,IDC_EDIT_TOBOX);
	std::wstring orignal;
	int szOrignalLength = ::GetWindowTextLengthW(hEditToBox);

	if(szOrignalLength != 0){
		WCHAR* szText = new WCHAR[szOrignalLength+1];
		::GetWindowTextW(hEditToBox,szText,szOrignalLength+1);
		orignal = szText;
		delete[] szText;
		if(orignal.substr(orignal.length()-1,1) != L";") orignal.append(L";");
	}
	
	orignal.append(append);
	::SetWindowTextW(hEditToBox,orignal.c_str());
}

LRESULT CContactsForm::EnumSelectitemInContactList()
{
	HWND hListView = ::GetDlgItem(m_hWnd,IDC_LIST_CONTACT);
	int nSelCount = ListView_GetSelectedCount(hListView);
	if(nSelCount == 0) return 0;
	int nNextSelectedIndex = -1;
	std::wstring appendto;
	LVITEMW item = {0};
	item.mask = LVIF_TEXT;
	item.cchTextMax = 256;
	item.iSubItem = 1;
	item.pszText = new WCHAR[256];
	nNextSelectedIndex = ListView_GetNextItem(hListView,nNextSelectedIndex,LVNI_SELECTED);
	while(nNextSelectedIndex != -1){
		item.iItem = nNextSelectedIndex;
		ListView_GetItem(hListView,&item);
		appendto.append(item.pszText);
		appendto.append(L";");
		nNextSelectedIndex = ListView_GetNextItem(hListView,nNextSelectedIndex,LVNI_SELECTED);
	}
	delete []item.pszText;
	item.pszText = NULL;

	AppendMobileNumberToBox(appendto.c_str());
	return 0;
}

LRESULT CContactsForm::OnClickedOK(WORD wNotifyCode, WORD wID, HWND hWndCtl, BOOL& bHandled)
{
	mobiles.clear();
	HWND hEditToBox = ::GetDlgItem(m_hWnd,IDC_EDIT_TOBOX);
	int szOrignalLength = ::GetWindowTextLengthW(hEditToBox);
	if(szOrignalLength != 0){
		WCHAR* szText = new WCHAR[szOrignalLength+1];
		::GetWindowTextW(hEditToBox,szText,szOrignalLength+1);
		mobiles = szText;
		delete[] szText;
	}
	EndDialog(wID);
	return 0;
}

LRESULT CContactsForm::GetAppendContactList(std::wstring& mobiles)
{
	mobiles = this->mobiles;
	return 0;
}